﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using com.vs.General;
using com.vs.DataAccess;

namespace com.vs.DataAccess.DO
{
    public partial class ReceiptLocateRequestDO : BaseDO
    {
        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static ReceiptLocateRequestDO GetModelByPK(Session sess, double iIntShpAllocNum)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            StringBuilder sbSQL = null;

            try
            {
                sbSQL = new StringBuilder(SelectSequel).Append(" WHERE INTERNAL_REC_LOC_NUM = @INTERNAL_REC_LOC_NUM ");
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, sbSQL.ToString(), new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_REC_LOC_NUM", iIntShpAllocNum)
                });
                if (!DataManager.IsEmpty(dtModel))
                {
                    return GetModel(dtModel.Rows[0]);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据，同时自增长属性INTERNAL_SHIP_ALLOC_NUM也会同时得到更新,并提供数据库事务支持
        /// </summary>
        /// <remarks></remarks>
        public string Insert(Session sess)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "RCP_IReceiptLocReq";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(this);
                return "" + helper.GetInt(CommandType.StoredProcedure, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public void Delete(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "DELETE FROM RECEIPT_LOCATE_REQUEST WHERE INTERNAL_REC_LOC_NUM = @INTERNAL_REC_LOC_NUM";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.Text, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_REC_LOC_NUM", this.INTERNAL_REC_LOC_NUM)
                });
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public void Update(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "RCP_UReceiptLocReq";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(this));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }


        }



        public string Update(Session sess, Dictionary<string, object> dcData)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            string stSQL = null;
            int i = 0;
            IDataParameter[] paras = null;

            try
            {
                if (dcData.Count < 1)
                    return null;
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(256)
                            .Append(" UPDATE RECEIPT_LOCATE_REQUEST SET ");
                paras = new IDataParameter[dcData.Count + 1];
                foreach (KeyValuePair<string, object> kvp in dcData)
                {
                    sbSQL.Append(kvp.Key).Append(" = @Value").Append(i).Append(",");
                    paras[i] = new SqlParameter("@Value" + i, kvp.Value);
                    i++;
                }

                stSQL = sbSQL.ToString();
                stSQL = stSQL.Substring(0, stSQL.Length - 1);

                stSQL = stSQL + " WHERE INTERNAL_REC_LOC_NUM = @INTERNAL_REC_LOC_NUM";
                paras[i] = new SqlParameter("@INTERNAL_REC_LOC_NUM", this.INTERNAL_REC_LOC_NUM);

                helper.Update(CommandType.Text, stSQL, paras);

                return null;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象SHIPMENALLOCREQUEST的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static List<ReceiptLocateRequestDO> GetAll(Session sess)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = SelectSequel;
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.Text, stSQL);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性SHIPMENALLOCREQUEST对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static List<ReceiptLocateRequestDO> GetListByColumn(Session sess, string columnName, Object value, string orderByClause)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumn(sess, columnName, value, orderByClause);
                if (DataManager.IsEmpty(dtModel))
                    return null;
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }

        public static List<ReceiptLocateRequestDO> GetListByColumns(Session sess, Dictionary<string, Object> dcParam)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetTableByColumns(sess, dcParam);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
            }
        }

        public static DataTable GetTableByColumn(Session sess, string columnName, Object value, string orderByClause)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = SelectSequel + " Where [" + columnName + "] =@Value ";
                if (!StringManager.IsEmpty(orderByClause))
                    stSQL = stSQL + " Order By " + orderByClause;

                paras = new IDataParameter[]{
                    new SqlParameter( "@Value", value)
                };
                return helper.GetTable(CommandType.Text, stSQL, paras);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public static DataTable GetTableByColumns(Session sess, Dictionary<string, Object> dcParam)
        {
            DataHelper helper = null;
            StringBuilder sbSQL = null;
            int i = 0;
            IDataParameter[] pars = null;

            try
            {
                helper = new DataHelper(sess);
                sbSQL = new StringBuilder(SelectSequel)
                    .Append(" WHERE 1=1 ");

                pars = new IDataParameter[dcParam.Count];
                foreach (KeyValuePair<string, Object> de in dcParam)
                {
                    if (de.Value != null)
                    {
                        sbSQL.Append(" AND ").Append(de.Key.ToString()).Append(" = @Value").Append(i);
                        pars[i] = new SqlParameter("@Value" + i, de.Value);
                        i++;
                    }
                }
                return helper.GetTable(CommandType.Text, sbSQL.ToString(), pars);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 从DataView装载持久性SHIPMENALLOCREQUEST对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<ReceiptLocateRequestDO> LoadListFromDataView(DataView dv)
        {
            List<ReceiptLocateRequestDO> list = new List<ReceiptLocateRequestDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }

        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        public static ReceiptLocateRequestDO GetModel(DataRow row)
        {
            ReceiptLocateRequestDO obj = new ReceiptLocateRequestDO(); 
            if (!row["INTERNAL_REC_LOC_NUM"].Equals(DBNull.Value)) obj.INTERNAL_REC_LOC_NUM = Convert.ToInt32(row["INTERNAL_REC_LOC_NUM"]);
            if (!row["WAREHOUSE"].Equals(DBNull.Value)) obj.WAREHOUSE = Convert.ToString(row["WAREHOUSE"]);
            if (!row["COMPANY"].Equals(DBNull.Value)) obj.COMPANY = Convert.ToString(row["COMPANY"]);
            if (!row["INTERNAL_CONTAINER_NUM"].Equals(DBNull.Value)) obj.INTERNAL_CONTAINER_NUM = Convert.ToInt32(row["INTERNAL_CONTAINER_NUM"]);
            if (!row["CONTAINER_ID"].Equals(DBNull.Value)) obj.CONTAINER_ID = Convert.ToString(row["CONTAINER_ID"]);
            if (!row["ITEM"].Equals(DBNull.Value)) obj.ITEM = Convert.ToString(row["ITEM"]);
            if (!row["ITEM_DESC"].Equals(DBNull.Value)) obj.ITEM_DESC = Convert.ToString(row["ITEM_DESC"]);
            if (!row["LOCATE_QTY"].Equals(DBNull.Value)) obj.LOCATE_QTY = Convert.ToInt32(row["LOCATE_QTY"]);
            if (!row["QUANTITY_UM"].Equals(DBNull.Value)) obj.QUANTITY_UM = Convert.ToString(row["QUANTITY_UM"]);
            if (!row["ATTRIBUTE_NUM"].Equals(DBNull.Value)) obj.ATTRIBUTE_NUM = Convert.ToInt32(row["ATTRIBUTE_NUM"]);
            if (!row["FROM_LOC"].Equals(DBNull.Value)) obj.FROM_LOC = Convert.ToString(row["FROM_LOC"]);
            if (!row["TO_LOC"].Equals(DBNull.Value)) obj.TO_LOC = Convert.ToString(row["TO_LOC"]);
            if (!row["FROM_TEMPLATE_FIELD1"].Equals(DBNull.Value)) obj.FROM_TEMPLATE_FIELD1 = Convert.ToString(row["FROM_TEMPLATE_FIELD1"]);
            if (!row["FROM_TEMPLATE_FIELD2"].Equals(DBNull.Value)) obj.FROM_TEMPLATE_FIELD2 = Convert.ToString(row["FROM_TEMPLATE_FIELD2"]);
            if (!row["FROM_TEMPLATE_FIELD3"].Equals(DBNull.Value)) obj.FROM_TEMPLATE_FIELD3 = Convert.ToString(row["FROM_TEMPLATE_FIELD3"]);
            if (!row["FROM_TEMPLATE_FIELD4"].Equals(DBNull.Value)) obj.FROM_TEMPLATE_FIELD4 = Convert.ToString(row["FROM_TEMPLATE_FIELD4"]);
            if (!row["FROM_TEMPLATE_FIELD5"].Equals(DBNull.Value)) obj.FROM_TEMPLATE_FIELD5 = Convert.ToString(row["FROM_TEMPLATE_FIELD5"]);
            if (!row["TO_TEMPLATE_FIELD1"].Equals(DBNull.Value)) obj.TO_TEMPLATE_FIELD1 = Convert.ToString(row["TO_TEMPLATE_FIELD1"]);
            if (!row["TO_TEMPLATE_FIELD2"].Equals(DBNull.Value)) obj.TO_TEMPLATE_FIELD2 = Convert.ToString(row["TO_TEMPLATE_FIELD2"]);
            if (!row["TO_TEMPLATE_FIELD3"].Equals(DBNull.Value)) obj.TO_TEMPLATE_FIELD3 = Convert.ToString(row["TO_TEMPLATE_FIELD3"]);
            if (!row["TO_TEMPLATE_FIELD4"].Equals(DBNull.Value)) obj.TO_TEMPLATE_FIELD4 = Convert.ToString(row["TO_TEMPLATE_FIELD4"]);
            if (!row["TO_TEMPLATE_FIELD5"].Equals(DBNull.Value)) obj.TO_TEMPLATE_FIELD5 = Convert.ToString(row["TO_TEMPLATE_FIELD5"]);
            if (!row["ZONE"].Equals(DBNull.Value)) obj.ZONE = Convert.ToString(row["ZONE"]);
            if (!row["CONVERTED_QTY"].Equals(DBNull.Value)) obj.CONVERTED_QTY = Convert.ToInt32(row["CONVERTED_QTY"]);
            if (!row["CONVERTED_QTY_UM"].Equals(DBNull.Value)) obj.CONVERTED_QTY_UM = Convert.ToString(row["CONVERTED_QTY_UM"]);
            if (!row["TASK_CREATED"].Equals(DBNull.Value)) obj.TASK_CREATED = Convert.ToString(row["TASK_CREATED"]);
            if (!row["RECEIPT_ID"].Equals(DBNull.Value)) obj.RECEIPT_ID = Convert.ToString(row["RECEIPT_ID"]);
            if (!row["RECEIPT_TYPE"].Equals(DBNull.Value)) obj.RECEIPT_TYPE = Convert.ToString(row["RECEIPT_TYPE"]);
            if (!row["RECEIPT_DATE"].Equals(DBNull.Value)) obj.RECEIPT_DATE = Convert.ToDateTime(row["RECEIPT_DATE"]);
            if (!row["PARENT_CONTAINER_NUM"].Equals(DBNull.Value)) obj.PARENT_CONTAINER_NUM = Convert.ToInt32(row["PARENT_CONTAINER_NUM"]);
            if (!row["PARENT_CONTAINER_ID"].Equals(DBNull.Value)) obj.PARENT_CONTAINER_ID = Convert.ToString(row["PARENT_CONTAINER_ID"]);
            if (!row["INTERNAL_RECEIPT_NUM"].Equals(DBNull.Value)) obj.INTERNAL_RECEIPT_NUM = Convert.ToInt32(row["INTERNAL_RECEIPT_NUM"]);
            if (!row["INTERNAL_RECEIPT_LINE_NUM"].Equals(DBNull.Value)) obj.INTERNAL_RECEIPT_LINE_NUM = Convert.ToInt32(row["INTERNAL_RECEIPT_LINE_NUM"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) obj.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) obj.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);
            if (!row["INVENTORY_STS"].Equals(DBNull.Value)) obj.INVENTORY_STS = Convert.ToString(row["INVENTORY_STS"]);
            if (!row["INTERNAL_WAVE_NUM"].Equals(DBNull.Value)) obj.INTERNAL_WAVE_NUM = Convert.ToInt32(row["INTERNAL_WAVE_NUM"]);

            return obj;
        }

        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        private static string SelectSequel
        {
            get
            {
                return "Select *  From [RECEIPT_LOCATE_REQUEST] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDbDataParameter[] ValueParas(ReceiptLocateRequestDO obj)
        {
            SqlParameter[] paras = new SqlParameter[37];
            paras[0] = new SqlParameter("@INTERNAL_REC_LOC_NUM", obj.INTERNAL_REC_LOC_NUM);
            paras[0].DbType = DbType.Int32;
            paras[1] = new SqlParameter("@WAREHOUSE", obj.WAREHOUSE == String.Empty ? DBNull.Value : (object)obj.WAREHOUSE);
            paras[1].DbType = DbType.String;
            paras[2] = new SqlParameter("@COMPANY", obj.COMPANY == String.Empty ? DBNull.Value : (object)obj.COMPANY);
            paras[2].DbType = DbType.String;
            paras[3] = new SqlParameter("@INTERNAL_CONTAINER_NUM", obj.INTERNAL_CONTAINER_NUM);
            paras[3].DbType = DbType.Int32;
            paras[4] = new SqlParameter("@CONTAINER_ID", obj.CONTAINER_ID == String.Empty ? DBNull.Value : (object)obj.CONTAINER_ID);
            paras[4].DbType = DbType.String;
            paras[5] = new SqlParameter("@ITEM", obj.ITEM == String.Empty ? DBNull.Value : (object)obj.ITEM);
            paras[5].DbType = DbType.String;
            paras[6] = new SqlParameter("@ITEM_DESC", obj.ITEM_DESC == String.Empty ? DBNull.Value : (object)obj.ITEM_DESC);
            paras[6].DbType = DbType.String;
            paras[7] = new SqlParameter("@LOCATE_QTY", obj.LOCATE_QTY);
            paras[7].DbType = DbType.Int32;
            paras[8] = new SqlParameter("@QUANTITY_UM", obj.QUANTITY_UM == String.Empty ? DBNull.Value : (object)obj.QUANTITY_UM);
            paras[8].DbType = DbType.String;
            paras[9] = new SqlParameter("@ATTRIBUTE_NUM", obj.ATTRIBUTE_NUM);
            paras[9].DbType = DbType.Int32;
            paras[10] = new SqlParameter("@FROM_LOC", obj.FROM_LOC == String.Empty ? DBNull.Value : (object)obj.FROM_LOC);
            paras[10].DbType = DbType.String;
            paras[11] = new SqlParameter("@TO_LOC", obj.TO_LOC == String.Empty ? DBNull.Value : (object)obj.TO_LOC);
            paras[11].DbType = DbType.String;
            paras[12] = new SqlParameter("@FROM_TEMPLATE_FIELD1", obj.FROM_TEMPLATE_FIELD1 == String.Empty ? DBNull.Value : (object)obj.FROM_TEMPLATE_FIELD1);
            paras[12].DbType = DbType.String;
            paras[13] = new SqlParameter("@FROM_TEMPLATE_FIELD2", obj.FROM_TEMPLATE_FIELD2 == String.Empty ? DBNull.Value : (object)obj.FROM_TEMPLATE_FIELD2);
            paras[13].DbType = DbType.String;
            paras[14] = new SqlParameter("@FROM_TEMPLATE_FIELD3", obj.FROM_TEMPLATE_FIELD3 == String.Empty ? DBNull.Value : (object)obj.FROM_TEMPLATE_FIELD3);
            paras[14].DbType = DbType.String;
            paras[15] = new SqlParameter("@FROM_TEMPLATE_FIELD4", obj.FROM_TEMPLATE_FIELD4 == String.Empty ? DBNull.Value : (object)obj.FROM_TEMPLATE_FIELD4);
            paras[15].DbType = DbType.String;
            paras[16] = new SqlParameter("@FROM_TEMPLATE_FIELD5", obj.FROM_TEMPLATE_FIELD5 == String.Empty ? DBNull.Value : (object)obj.FROM_TEMPLATE_FIELD5);
            paras[16].DbType = DbType.String;
            paras[17] = new SqlParameter("@TO_TEMPLATE_FIELD1", obj.TO_TEMPLATE_FIELD1 == String.Empty ? DBNull.Value : (object)obj.TO_TEMPLATE_FIELD1);
            paras[17].DbType = DbType.String;
            paras[18] = new SqlParameter("@TO_TEMPLATE_FIELD2", obj.TO_TEMPLATE_FIELD2 == String.Empty ? DBNull.Value : (object)obj.TO_TEMPLATE_FIELD2);
            paras[18].DbType = DbType.String;
            paras[19] = new SqlParameter("@TO_TEMPLATE_FIELD3", obj.TO_TEMPLATE_FIELD3 == String.Empty ? DBNull.Value : (object)obj.TO_TEMPLATE_FIELD3);
            paras[19].DbType = DbType.String;
            paras[20] = new SqlParameter("@TO_TEMPLATE_FIELD4", obj.TO_TEMPLATE_FIELD4 == String.Empty ? DBNull.Value : (object)obj.TO_TEMPLATE_FIELD4);
            paras[20].DbType = DbType.String;
            paras[21] = new SqlParameter("@TO_TEMPLATE_FIELD5", obj.TO_TEMPLATE_FIELD5 == String.Empty ? DBNull.Value : (object)obj.TO_TEMPLATE_FIELD5);
            paras[21].DbType = DbType.String;
            paras[22] = new SqlParameter("@ZONE", obj.ZONE == String.Empty ? DBNull.Value : (object)obj.ZONE);
            paras[22].DbType = DbType.String;
            paras[23] = new SqlParameter("@CONVERTED_QTY", obj.CONVERTED_QTY);
            paras[23].DbType = DbType.Int32;
            paras[24] = new SqlParameter("@CONVERTED_QTY_UM", obj.CONVERTED_QTY_UM == String.Empty ? DBNull.Value : (object)obj.CONVERTED_QTY_UM);
            paras[24].DbType = DbType.String;
            paras[25] = new SqlParameter("@TASK_CREATED", obj.TASK_CREATED == String.Empty ? DBNull.Value : (object)obj.TASK_CREATED);
            paras[25].DbType = DbType.String;
            paras[26] = new SqlParameter("@RECEIPT_ID", obj.RECEIPT_ID == String.Empty ? DBNull.Value : (object)obj.RECEIPT_ID);
            paras[26].DbType = DbType.String;
            paras[27] = new SqlParameter("@RECEIPT_TYPE", obj.RECEIPT_TYPE == String.Empty ? DBNull.Value : (object)obj.RECEIPT_TYPE);
            paras[27].DbType = DbType.String;
            paras[28] = new SqlParameter("@RECEIPT_DATE", obj.RECEIPT_DATE);
            paras[28].DbType = DbType.DateTime;
            paras[29] = new SqlParameter("@PARENT_CONTAINER_NUM", obj.PARENT_CONTAINER_NUM);
            paras[29].DbType = DbType.Int32;
            paras[30] = new SqlParameter("@PARENT_CONTAINER_ID", obj.PARENT_CONTAINER_ID == String.Empty ? DBNull.Value : (object)obj.PARENT_CONTAINER_ID);
            paras[30].DbType = DbType.String;
            paras[31] = new SqlParameter("@INTERNAL_RECEIPT_NUM", obj.INTERNAL_RECEIPT_NUM);
            paras[31].DbType = DbType.Int32;
            paras[32] = new SqlParameter("@INTERNAL_RECEIPT_LINE_NUM", obj.INTERNAL_RECEIPT_LINE_NUM);
            paras[32].DbType = DbType.String;
            paras[33] = new SqlParameter("@USER_STAMP", obj.USER_STAMP == String.Empty ? DBNull.Value : (object)obj.USER_STAMP);
            paras[33].DbType = DbType.String;
            paras[34] = new SqlParameter("@DATE_TIME_STAMP", System.DateTime.Now);
            paras[34].DbType = DbType.DateTime;
            paras[35] = new SqlParameter("@INVENTORY_STS", obj.INVENTORY_STS == String.Empty ? DBNull.Value : (object)obj.INVENTORY_STS);
            paras[35].DbType = DbType.String;
            paras[36] = new SqlParameter("@INTERNAL_WAVE_NUM", obj.INTERNAL_WAVE_NUM);
            paras[36].DbType = DbType.Int32;



            return paras;
        }

        private IDbDataParameter[] WherePara()
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@INTERNAL_REC_LOC_NUM", this.INTERNAL_REC_LOC_NUM);
            return paras;
        }

        #endregion

    }
}
